/******************************************************************************/
/*!
\file Window.cpp
\author David Seah Seng Chong
\par email: sengchong.seah\@digipen.edu
\par DigiPen login: sengchong.seah
\par Course: GAM200
\date 21/10/2011
\brief
Header file
Graphics Engine

Functions included in file:\n
*/
/******************************************************************************/
//#include "Global_Variables.hpp"
#include "PreCompiledHeader.h"
#include "Window.hpp"
#include "System.hpp"
#include "GameStateMgr.hpp"

LPCTSTR szClassName = TEXT("ASTEROID");
    
Window::Window()
{
    m_hWnd = 0;
    m_hInstance = 0;
    
}

bool Window::Initialize()
{
    m_hInstance = GetModuleHandle(NULL);

    // this struct holds information for the window class
	WNDCLASSEX wc;

    // clear out the window class for use
    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    // fill in the struct with the needed information
    wc.cbSize = sizeof(WNDCLASSEX);	
    wc.style = NULL; // Style of the Window
    wc.lpfnWndProc = WindowProc; //Message Handler to use
    wc.cbClsExtra = 0; //Extra bytes to assign
    wc.cbWndExtra = 0; //Extra bytes to assign
    wc.hInstance = m_hInstance; //Thread where this window will be created
    wc.hIcon = LoadIconA(NULL, IDI_WINLOGO); // The big icon for this window, appear when Alt -Tab
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground =  NULL; //Background color
    wc.lpszClassName = szClassName; //Name to define to this new window class

    // register the window class
    RegisterClassEx(&wc);

    //screen width
    int cx = GetSystemMetrics(SM_CXSCREEN);
    cx /= 2;
    // screen height
    int cy = GetSystemMetrics(SM_CYSCREEN);
    cy /=2 ;
    // create the window and use the result as the handle
    m_hWnd = CreateWindowEx(NULL,
        szClassName,   // name of the window class
        szClassName,   // title of the window
        WS_CAPTION | WS_OVERLAPPED | WS_SYSMENU | WS_VISIBLE,    // window style
        0,    // x-position of the window
        0,    // y-position of the window
        SYSTEM->GetWindowWidth(),    // width of the window
        SYSTEM->GetWindowHeight(),    // height of the window
        NULL,    // we have no parent window, NULL
        NULL,    // we aren't using menus, NULL
        m_hInstance,    // application handle
        NULL);    // used with multiple windows, NULL
        
    if(m_hWnd == NULL)
    {
        MessageBox(NULL, TEXT("Window Initialization FAILED!."), "ERROR", MB_OK);
        SendMessage(NULL, WM_QUIT, 0, 0);
        return false;
    }

    ShowWindow(m_hWnd, SW_SHOW);
   
    return true;
}

void Window::UpdateWindow()
{
    MSG Msg;
    ZeroMemory(&Msg, sizeof(MSG));
    while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
    {
        if (Msg.message == WM_QUIT)
        {
           gGameStateNext = GS_QUIT;
        }
        else
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
    }
}

void Window::ShutdownWindow()
{
    if (m_hWnd)
    {
        if (!DestroyWindow(m_hWnd))
        {
            MessageBox(NULL, TEXT("Window Destruction FAILED!."), TEXT("ERROR"), MB_OK);
            SendMessage(NULL, WM_QUIT, 0, 0);
        }
        m_hWnd = NULL;
    }

    if (!UnregisterClass( szClassName, m_hInstance))
    {
        MessageBox(NULL, TEXT("Could not unregister the class."), TEXT("ERROR"), MB_OK);
        SendMessage(NULL, WM_QUIT, 0, 0);
    }

    m_hInstance = NULL;
}

LRESULT CALLBACK Window::WindowProc(HWND hWnd,UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
    {
    case WM_CREATE:
        break;

    case WM_KEYDOWN:
        break;

    case WM_KEYUP:
        break;

    case WM_LBUTTONDOWN:
        break;

    case WM_MOUSEMOVE:
        break;

    case WM_LBUTTONUP:
        break;
        
    case WM_CLOSE:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }

    return 0;
}




